using System;
using System.Runtime.Serialization;

namespace Pegasus.UnitTests.Runtime.Serialization.Formatters.Xml.XmlFormatter2Tests
{
	/// <summary>
	/// This object knows how to serialize/deserialize the NonserializationStruct class.
	/// </summary>
	public class StructSerializationSurrogate : ISerializationSurrogate
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="T:StructSerializationSurrogate"/> class.
		/// </summary>
		public StructSerializationSurrogate()
		{
		}

		/// <summary>
		/// Populates the provided <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> with the data needed to serialize the object.
		/// </summary>
		/// <param name="obj">The object to serialize.</param>
		/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> to populate with data.</param>
		/// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"></see>) for this serialization.</param>
		/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
		public void GetObjectData( object obj, SerializationInfo info, StreamingContext context )
		{
			NonserializationStruct child = (NonserializationStruct) obj;
			info.AddValue( "Name", child.Name );
			info.AddValue( "Description", child.Description );
			info.AddValue( "Count", child.Count );
		}

		/// <summary>
		/// Populates the object using the information in the <see cref="T:System.Runtime.Serialization.SerializationInfo"></see>.
		/// </summary>
		/// <param name="obj">The object to populate.</param>
		/// <param name="info">The information to populate the object.</param>
		/// <param name="context">The source from which the object is deserialized.</param>
		/// <param name="selector">The surrogate selector where the search for a compatible surrogate begins.</param>
		/// <returns>The populated deserialized object.</returns>
		/// <exception cref="T:System.Security.SecurityException">The caller does not have the required permission. </exception>
		public object SetObjectData( object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector )
		{
			NonserializationStruct child = (NonserializationStruct) obj;
			child.Name = info.GetString( "Name" );
			child.Description = info.GetString( "Description" );
			child.Count = info.GetInt32( "Count" );

			return child;
		}
	}
}
